<?php
namespace Admin\Controller;
class AdminsController extends AdminController{

   protected function _initialize() {
		parent::_initialize ();
	}
	/**
	 * 身份列表
	 */
	public function index($p = 1)
	{
		$p        = intval($p) > 0 ? $p : 1;
		$admins   = M('admins a');
		$pagesize = 15;#每页数量
		$offset   = $pagesize * ($p - 1);//计算记录偏移量

		$where = 'a.gid = g.id';
		$where .= I('get.gid') > 0 ? ' and a.gid=' . I('get.gid', 0, 'intval') : '';
		$where .= I('get.name') ? ' and a.name like "%' . I('get.name') . '%"' : '';
		$where .= I('get.tel') ? ' and a.tel like "%' . I('get.tel') . '%"' : '';
		$where .= I('get.status', -1) > -1 ? ' and a.status =' . I('get.status') : '';

		$count = $admins->join('app_group g on a.gid = g.id')->where($where)->count();
		$list  = $admins->field('a.*,g.name groupname')->join('app_group g on a.gid = g.id')->where($where)->order('id desc')->limit($offset . ',' . $pagesize)->select();

        foreach ($list as $key => $value)
		{
			if($value['gpid']!=0)
			{
              $pname=$this->getInfo('*','group','id='.$value['gpid']);
                $list[$key]['pname']=$pname['name'];
			}else{
			   $list[$key]['pname']='无';
			}

		}

		$page      = new \Think\Page($count, $pagesize);
		$page      = $page->show();
		$groupList = $this->getList('id,name', 'group', 'status=1', 'id desc',true);
		$this->assign('list', $list);
		$this->assign('groupList', $groupList);
		$this->assign('page', $page);
		$this->display();
	}

	/**
	 * 员工列表
	 */
	public function add()
	{
		if(IS_POST)
		{
			if($_POST['pwd'] != $_POST['pwd2'])
			{
				$this->returnResult(false, '两次密码不一致');
			}
			$_POST['pwd'] = md5($_POST['name'] . '_comMunIty_' . $_POST['pwd']);
			$_POST['bids'] = implode(',', $_POST['bid']);
			$gpid=$this->getInfo('*','group','id='.$_POST['gid']);
			$_POST['gpid']=$gpid['pid'];
			/*$adminsInfo=M('admins')->where(array('name'=>$_POST['name'],''))->find();*/
			$adminInfo=$this->getInfo('*','admins','name='."'".$_POST['name']."'".' or tel='."'".$_POST['tel']."'");
			if($adminInfo)
			{
                $this->error('该信息已存在');
			}
			$result = $this->updateData($_POST, 'admins');
			$this->returnResult($result);
		}else{
			/*$groupList = $this->getList('id,name', 'group', 'status = 1', 'id desc');
			$this->assign('groupList', $groupList);*/
			$list = $this->getList('*', 'group', 'status=1', 'id asc');
			$bankList=$this->getList('*','bank','status=0');
			foreach($list as $key => $value)
			{
			if($value['pid'] == 0)
			{
				$arr[] = $value;
				foreach($list as $k => $v)
				{
					if($v['pid'] == $value['id'])
					{
						$arr[] = $v;
						foreach($list as $k1 => $v1)
						{
							if($v1['pid'] == $v['id'])
							{
								$arr[] = $v1;
							}
						}
					}
				}
			}
		   }

		    $this->assign('bankList', $bankList);
			$this->assign('nodeList', $arr);
			$this->display();
		}
	}

	/**
	 * 身份列表
	 */
	public function edit()
	{
		if(IS_POST)
		{
			if(!empty($_POST['pwd']))
			{
				if($_POST['pwd'] != $_POST['pwd2'])
				{
					$this->returnResult(false, '两次密码不一致');
				}
				$_POST['pwd'] = md5($_POST['name'] . '_comMunIty_' . $_POST['pwd']);
			} else {
				unset($_POST['pwd']);
			}
			$_POST['bids'] = implode(',', $_POST['bid']);
			$gpid          = $this->getInfo('*','group','id='.$_POST['gid']);
			$_POST['gpid'] = $gpid['pid'];

			// $adminInfo=$this->getInfo('*','admins','name='."'".$_POST['name']."'".' or tel='."'".$_POST['tel']."'");
			// if($adminInfo)
			// {
   //              $this->error('该信息已存在');
			// }
			$result = $this->updateData($_POST, 'admins', 2);
			$this->returnResult($result);
		}
		else
		{
			$info         = $this->getInfo('*', 'admins', 'id=' . I('get.id', 0, 'intval'));
			$info['bids'] = explode(',', $info['bids']);
			$bankList     = $this->getList('*','bank','status=0');
			$list         = $this->getList('*', 'group', 'status=1', 'id asc');
			foreach($list as $key => $value)
			{
				if($value['pid'] == 0)
				{
					$arr[] = $value;
					foreach($list as $k => $v)
					{
						if($v['pid'] == $value['id'])
						{
							$arr[] = $v;
							foreach($list as $k1 => $v1)
							{
								if($v1['pid'] == $v['id'])
								{
									$arr[] = $v1;
								}
							}
						}
					}
				}
		   }
		   $this->assign('info', $info);
		   $this->assign('bankList', $bankList);
		   $this->assign('nodeList', $arr);
		   $this->display('add');
		}
	}

	/**
	 * 身份列表
	 */
	public function del()
	{
		//$result = M('admins')->where('id=' . I('get.id', 0, 'intval'))->setField('status', 2);
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'admins');
		$this->returnResult($result);
	}
}